
<?php
include_once 'EditArticlePresenter.php';
include_once 'AbstractView.php';
include_once 'InsultBuilder.php';
class EditArticleView extends AbstractView {
	private $presenter;
	private $articleId;
	private $article;
	public function __construct($articleId) {
		parent::__construct ();
		$this->articleId = $articleId;
		$this->presenter = new EditArticlePresenter ( $this, parent::getContentRepository () );
		
		$this->article = parent::getContentRepository ()->getArticle ( $articleId );
	}
	public function getArticleId () {
		return $this->articleId;
	}
	public function editArticleFailed($reason) {
		$builder = new InsultBuilder ();
		$builder->setMessage ( $reason )->setTitle ( "Dude, you messed up!" )->setPicture ( "editarticle_failed.jpg" )->setSource ( "readarticle.php?article=" . $articleId );
		parent::redirect ( $builder->build () );
	}
	public function editArticleSucceed($articleId) {
		$builder = new InsultBuilder ();
		$builder->setMessage ( "You made it! Go grab yourself a pie, dear." )->setTitle ( "Fine, it's through." )->setPicture ( "editarticle_succeed.jpg" )->setSource ( "readarticle.php?article=" . $articleId );
		parent::redirect ( $builder->build () );
	}
	public function render() {
		if (! parent::invokedByAdmin ()) {
			parent::block ();
			return;
		}
		
		if (isset ( $_POST ["title"] ) || isset ( $_POST ["content"] )) {
			
			$this->presenter->postArticle ( $_POST ["title"], $_POST ["content"], parent::getActiveUser ()->getuserName () );
			//echo "This message is coming from EditArticleView::render()";
			return;
		}
		
		require 'EditArticle.phtml';
	}
}

$view = new EditArticleView ($_GET['article']);
$view->render ();